from google.appengine.ext import db
from google.appengine.ext import search

# Page cache
class Page(db.Model):
  identifier = db.StringProperty() # Board front pages are stored as 'front' for page 0, and 'front<n>' for each page thereafter.  Res pages are stored as the thread OP's datstore key
  contents = db.BlobProperty()

# Google account tracking and preferences
class UserPrefs(db.Model):
  user = db.UserProperty() # User object
  level = db.CategoryProperty(default='Normal') # TODO: Access level
  name_override = db.StringProperty(default='')
  posts = db.IntegerProperty(default=0) # Number of posts this user has made
  posts_sage = db.IntegerProperty(default=0) # Number of sage posts this user has made

class Ban(db.Model):
  ip = db.StringProperty()
  reason = db.StringProperty()
  placed = db.DateTimeProperty()

class Image(db.Model):
  data = db.BlobProperty()
  thumb_data = db.BlobProperty()
  thumb_catalog_data = db.BlobProperty()
  attachedpost = db.StringProperty()

class Post(search.SearchableModel):
  postid = db.IntegerProperty() # Consecutive ID
  parentid = db.IntegerProperty() # Parent of post, <None> if this post has no parent
  author = db.UserProperty() # Google account used when making the post
  posts = db.IntegerProperty() # Number of posts in a thread (this includes the OP)
  image = db.StringProperty() # File attachment to post, stored here as a key to the actual instance, lowering memory when only fetching posts
  image_filename = db.StringProperty() # Full filename of the stored file ([timestamp + postid].ext)
  image_hex = db.StringProperty() # File hex for quick duplicate checks
  image_mime = db.StringProperty() # File MIME
  image_extension = db.StringProperty() # File extension (including period)
  image_original = db.StringProperty() # Original filename when the file was uploaded
  image_size = db.IntegerProperty() # Size of the file in bytes
  image_size_formatted = db.StringProperty() # Formatted version of size (KB)
  image_width = db.IntegerProperty() # Width of image
  image_height = db.IntegerProperty() # Height of image
  thumb_filename = db.StringProperty() # Full filename of the stored file thumbnail ([timestamp + postid]s.ext)
  thumb_width = db.IntegerProperty() # Width of thumbnail
  thumb_height = db.IntegerProperty() #height of thumbnail
  thumb_catalog_width = db.IntegerProperty() # Width of thumbnail (catalog)
  thumb_catalog_height = db.IntegerProperty() #height of thumbnail (catalog)
  ip = db.StringProperty() # IP address used when making the post
  nameblock = db.TextProperty() # Preprocessed HTML block containing the name, tripcode, email, and formatted date of the post
  anonymous = db.BooleanProperty() # Flag regarding if the poster's Google account should be displayed in the post, however it can also remove anything entered in the Name field
  name = db.StringProperty()
  tripcode = db.StringProperty()
  email = db.StringProperty()
  subject = db.StringProperty()
  message = db.TextProperty()
  password = db.StringProperty() # Stored as a hash
  date = db.DateTimeProperty(auto_now_add=True) # Datetime of when post was made
  date_formatted = db.StringProperty() # Preprocessed formatting of the datetime
  bumped = db.DateTimeProperty() # Datetime of when the post was bumped
  aa = db.BooleanProperty() # Text art looks much better when displayed with the correct font
  deleted = db.BooleanProperty()
  
class Idx(db.Model):
  name = db.StringProperty(required=True)
  count = db.IntegerProperty(required=True)
